package com.nightpiter.controller.threads;

import android.os.Handler;

import com.nightpiter.model.MetroModel;

/**
 * MetroModelLoaderThread class.
 * 
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class MetroModelLoaderThread extends Thread {

	/**
	 * file path
	 */
	private String path;

	/**
	 * model to load data in
	 */
	private MetroModel model;

	/**
	 * resource id
	 */
	private int resId;

	/**
	 * indicates if we should load data from file or from resource
	 */
	private boolean readFromFile;

	/**
	 * handler from UI thread
	 */
	private Handler handler;

	/**
	 * default constructor
	 */
	public MetroModelLoaderThread(String path, MetroModel model, Handler handler) {
		super("MetroModelLoaderThread");
		this.path = path;
		this.model = model;
		readFromFile = true;
		this.handler = handler;
	}

	/**
	 * default constructor
	 */
	public MetroModelLoaderThread(int resId, MetroModel model, Handler handler) {
		super("MetroModelLoaderThread");
		this.resId = resId;
		this.model = model;
		readFromFile = false;
		this.handler = handler;
	}

	@Override
	public void run() {
		super.run();
		// TODO this try-catch introduced to prevent application crash on my old
		// machine. I do not know exactly causes of this bug
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
		}
		if (readFromFile) {
			model.load(this.path, this.handler);
		} else {
			model.load(this.resId, this.handler);
		}
	}

}
